package kin.json;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import kin.test.Us;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializer;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

public class MyBeanSerializerModifier extends BeanSerializerModifier {

	@Override
	public JsonSerializer<?> modifySerializer(SerializationConfig config,
			BeanDescription beanDesc, JsonSerializer<?> serializer) {
		//System.out.println("yyyyyyyyyyyyyyy");
		return super.modifySerializer(config, beanDesc, serializer);
	}

	@Override
	public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,List<BeanPropertyWriter> beanProperties) {
		
	for (int i = 0; i < beanProperties.size(); i++) {
        BeanPropertyWriter beanPropertyWriter = beanProperties.get(i);
        
        System.out.println("zzzzzzzzzzzz:"+beanPropertyWriter.getName());
        System.out.println("zzggggggggggggz:"+beanPropertyWriter.getPropertyType());
        Class type=beanPropertyWriter.getPropertyType();
       System.out.println();
        if(!type.isPrimitive()&&!type.isArray()&&"kin.test".equals(type.getPackage().getName())){
        beanPropertyWriter.assignSerializer(new CustomSerializer());
        }
        
    
        if(type.getInterfaces().length>0&&type.getInterfaces()[0]==Collection.class){
        	Class cls=(Class) ((ParameterizedType)beanPropertyWriter.getGenericPropertyType()).getActualTypeArguments()[0];
        	
        	if(cls==Us.class){
        		System.out.println("aaaaaaaaaa:"+cls.getSimpleName());
        	 beanPropertyWriter.assignSerializer(new CustomSerializer());
        	}
       
        }
        //beanProperties.set(i, beanPropertyWriter.assignNullSerializer(new CustomSerializer()));
       /* if (PropertyName.equals(beanPropertyWriter.getName())) {
            beanProperties.set(i, beanPropertyWriter.withSerializer(new CustomSerializer()));
        }*/
    }
	return beanProperties;
		//return super.changeProperties(config, beanDesc, beanProperties);
	}

}
